#include "jy901.h"
#include "stdio.h"

//陀螺仪处理函数
static void PI_TO_Ture(volatile const uint8_t *jy901_buf,pi_reveive_data *data);
static void USART5_JY901_Config(uint8_t *rx1_buf, uint8_t *rx2_buf, uint16_t dma_buf_num);

//陀螺仪数据
//static fp32 Angle_Z;
//static fp32 Gyro_Z;
static pi_reveive_data Data_receive;

//接收原始数据，为11个字节
static uint8_t PI_rx_buf[2][PI_RX_BUF_NUM];

/**
  * @brief  USART5 GPIO 配置,工作模式配置。115200 9-E-1 
  * @param  无
  * @retval 无
  */


void PI_init(void)
{
	//数据指针-电机指向设置结构体
//	Data_receive->motor_msg = get_step_motor_point();
//	Data_receive.first_receive = false;
	
	USART5_JY901_Config(PI_rx_buf[0], PI_rx_buf[1], PI_RX_BUF_NUM);
}




static void USART5_JY901_Config(uint8_t *rx1_buf, uint8_t *rx2_buf, uint16_t dma_buf_num)         //RX-PD2    TX-PC12
{

	/* -------------- Enable Module Clock Source ----------------------------*/
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_GPIOD, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);

	GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5); // PC12  usart3_tx
	GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);  // PD2   usart3_rx
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_Init(GPIOD, &GPIO_InitStructure);

	USART_DeInit(UART5);

	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_Init(UART5, &USART_InitStructure);

	USART_DMACmd(UART5, USART_DMAReq_Rx, ENABLE);

	// USART_ClearFlag(USART3, USART_FLAG_RXNE);
	// USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
	USART_ClearFlag(UART5, USART_FLAG_IDLE);
	USART_ITConfig(UART5, USART_IT_IDLE, ENABLE);

	USART_Cmd(UART5, ENABLE);

	/* ------------------ Configure USART3 NVIC ----------------------- -------------*/
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PI_NVIC;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	// DMA1 stream0 ch4
	/* ----------------------- Configure DMA -----------------------------------*/
	DMA_InitTypeDef DMA_InitStructure;
	DMA_DeInit(DMA1_Stream0);

	DMA_InitStructure.DMA_Channel = DMA_Channel_4;
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (UART5->DR);
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)rx1_buf;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
	DMA_InitStructure.DMA_BufferSize = dma_buf_num;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
	DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
	DMA_Init(DMA1_Stream0, &DMA_InitStructure);

	DMA_DoubleBufferModeConfig(DMA1_Stream0, (uint32_t)rx2_buf, DMA_Memory_0); // 双缓冲模式
	DMA_DoubleBufferModeCmd(DMA1_Stream0, ENABLE);
	DMA_Cmd(DMA1_Stream0, ENABLE);
}



void UART5_IRQHandler(void)
{

	
	if (USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
     {
         USART_ReceiveData(UART5);
     }
     else if (USART_GetITStatus(UART5, USART_IT_IDLE) != RESET)
     {
         static uint16_t this_time_rx_len = 0;
         USART_ReceiveData(UART5);

         if(DMA_GetCurrentMemoryTarget(DMA1_Stream0) == 0)
         {
             //重新设置DMA
             DMA_Cmd(DMA1_Stream0, DISABLE);
             this_time_rx_len = PI_RX_BUF_NUM - DMA_GetCurrDataCounter(DMA1_Stream0);
             DMA_SetCurrDataCounter(DMA1_Stream0, PI_RX_BUF_NUM);
             DMA1_Stream0->CR |= DMA_SxCR_CT;
             //清DMA中断标志
             DMA_ClearFlag(DMA1_Stream0, DMA_FLAG_TCIF4 | DMA_FLAG_HTIF4);
             DMA_Cmd(DMA1_Stream0, ENABLE);   
			 if(this_time_rx_len==8 )
				PI_TO_Ture(PI_rx_buf[0], &Data_receive);
             
         }
         else
         {
             //重新设置DMA
             DMA_Cmd(DMA1_Stream0, DISABLE);
             this_time_rx_len = PI_RX_BUF_NUM - DMA_GetCurrDataCounter(DMA1_Stream0);
             DMA_SetCurrDataCounter(DMA1_Stream0, PI_RX_BUF_NUM);
             DMA1_Stream0->CR &= ~(DMA_SxCR_CT);
             //清DMA中断标志
             DMA_ClearFlag(DMA1_Stream0, DMA_FLAG_TCIF4 | DMA_FLAG_HTIF4);
             DMA_Cmd(DMA1_Stream0, ENABLE);
                 //处理陀螺仪数据 
			 if(this_time_rx_len==8 )
				PI_TO_Ture(PI_rx_buf[1], &Data_receive);
         }
     }
	
}



static void PI_TO_Ture(volatile const uint8_t *jy901_buf,pi_reveive_data *data)
{

	int sum=0;
	int i;
	int x_red, y_red;
	int delta_x, delta_y;
	
	if(jy901_buf[0]!=PI_START_BYTE)        //帧头校验
		return;
	else {
//		switch(jy901_buf[1]) {
//			case RED_FRAME_IDENTI:
//				
//				if(jy901_buf[2] != RED_FRAME_LENTH)            //帧长校验
//					return;
//				
//				for(i=0; i<RED_FRAME_LENTH-1; i++)
//					sum+=jy901_buf[i];
//				sum=sum&0x00ff;
//				
//				if(jy901_buf[RED_FRAME_LENTH-1]!=sum)         //和校验
//					return;
//				
//				x_red = jy901_buf[3] | (jy901_buf[4]<<8);
//				y_red = jy901_buf[5] | (jy901_buf[6]<<8);
//				
//				
////				if(x_red!=0&&y_red!=0) {
//					data->red_point.x = x_red;
//					data->red_point.y = y_red;
////				}
//				
//				
//			break;
//			
//			case FIV_FRAME_IDENTI:
//				
//				if(jy901_buf[2] != FIV_FRAME_LENTH)            //帧长校验
//					return;
//				
//				for(i=0; i<FIV_FRAME_LENTH-1; i++)
//					sum+=jy901_buf[i];
//				sum=sum&0x00ff;
//				
//				if(jy901_buf[FIV_FRAME_LENTH-1]!=sum)         //和校验
//					return;
//				
//				for(i=0; i<4; i++) {
//					data->point[i].x = jy901_buf[3+4*i] | (jy901_buf[4+4*i]<<8);
//					data->point[i].y = jy901_buf[5+4*i] | (jy901_buf[6+4*i]<<8);
//				}
//				
//				data->center_point.x = jy901_buf[19] | (jy901_buf[20]<<8);
//				data->center_point.y = jy901_buf[21] | (jy901_buf[22]<<8);
//				
//			break;
//			
//			case RAB_FRAME_IDENTI:
//				printf("identiOK\n");
//				if(jy901_buf[2] != RAB_FRAME_LENTH)            //帧长校验
//					return;
////				printf("lenthOK\n");
//				for(i=0; i<RAB_FRAME_LENTH-1; i++)
//					sum+=jy901_buf[i];
//				sum=sum&0x00ff;
//				
//				if(jy901_buf[RAB_FRAME_LENTH-1]!=sum)         //和校验
//					return;
//				printf("sumOK\n");
			
				delta_x = jy901_buf[2] | (jy901_buf[3]<<8);
				delta_y = jy901_buf[4] | (jy901_buf[5]<<8);
				
				if(delta_x<=200||delta_x>=-200) {
					data->red_green_delta.x_delta = delta_x;
				}
				
				if(delta_y<=200||delta_y>=-200) {
					data->red_green_delta.y_delta = delta_y;
				}
//				printf("%d %d\n",delta_x,delta_y);
//				data->red_green_delta.y_delta = jy901_buf[5] | (jy901_buf[6]<<8);
				
				
//			break;
//				
//			default:
//				break;
//		}
	}
	
	
	
}


const pi_reveive_data * get_pi_data_point(void)
{
	return &Data_receive;
}



